import Vue from 'Vue'
import VueCookies from 'vue-cookies'
import API from '../api/api'
import { envTypeData } from '../utils/public'
Vue.use(VueCookies)

export default {
  state: {
    defaultActiveIndex: '0',
    show: 'vuex成功',
    username: VueCookies.get('username') ? VueCookies.get('username') : '',
    userLoginInfo: VueCookies.get('userLoginInfo')
      ? VueCookies.get('userLoginInfo')
      : {},
    envType: VueCookies.get('envType') ? VueCookies.get('envType') : '',
    currentClientId: VueCookies.get('currentClientId')
      ? VueCookies.get('currentClientId')
      : '',
    defaultEnvOfTwo: VueCookies.get('defaultEnvOfTwo')
      ? VueCookies.get('defaultEnvOfTwo')
      : 'master',
    baseInfo: JSON.parse(sessionStorage.getItem('baseInfo')),
    paneActiveName: sessionStorage.getItem('paneActiveName')
      ? sessionStorage.getItem('paneActiveName')
      : '0',
    envApi: sessionStorage.getItem('envApi'),
  },
  getters: {
    username: (state) => {
      return state.username
    },
    userLoginInfo: (state) => {
      return state.userLoginInfo
    },
    envType: (state) => {
      return state.envType
    },
    currentClientId: (state) => {
      return state.currentClientId
    },
    defaultEnvOfTwo: (state) => {
      return state.defaultEnvOfTwo
    },
    baseInfo: (state) => {
      return state.baseInfo
    },
    defaultActiveIndex: (state) => {
      return state.defaultActiveIndex
    },
    paneActiveName: (state) => {
      return state.paneActiveName
    },
    envApi: (state) => {
      return state.envApi
    },
  },
  mutations: {
    SET_ActiveIndex: (state, defaultActiveIndex) => {
      state.defaultActiveIndex = defaultActiveIndex
    },
    CHANGE_SETTING: (state, { key, value }) => {
      // console.log('lll', key, value)
      if (state.hasOwnProperty(key)) {
        state[key] = value
        VueCookies.set(key, value)
      }
    },
    SET_BASEINFO: (state, baseInfo) => {
      state.baseInfo = baseInfo
    },
    SET_PaneActiveName: (state, paneActiveName) => {
      state.paneActiveName = paneActiveName
      sessionStorage.setItem('paneActiveName', paneActiveName)
    },
    SET_ENVAPI: (state, envApi) => {
      state.envApi = envApi
      sessionStorage.setItem('envApi', envApi)
    },
  },
  actions: {
    changeSetting({ commit }, data) {
      commit('CHANGE_SETTING', data)
    },
    setBaseInfo({ commit }, baseInfo) {
      commit('SET_BASEINFO', baseInfo)
      sessionStorage.setItem('baseInfo', JSON.stringify(baseInfo))
    },
    synLogin() {
      return new Promise((resolve, reject) => {
        let _userLoginInfo = VueCookies.get('userLoginInfo')
        // const _testClientId = _userLoginInfo.testClientId
        // const _synLoginCheckCode = _userLoginInfo.synLoginCheckCode
        const _uniqueid = _userLoginInfo.uniqueid
        const firmParams = {
          accountId: _uniqueid, // 账号ID
          // clientId: _testClientId,
          // checkCode: _synLoginCheckCode, // 验证码
        }
        API.synLogin_test(firmParams)
          .then((result) => {
            if (!API.ApiRequest(result)) {
              return false
            }
            resolve('100')
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    defaultEnv({ commit, rootState }, EnvOfTwo) {
      // 环境初始化
      console.log('环境初始化')
      let envType = ''
      let defaultEnvOfTwo = 'master'
      let _userLoginInfo = rootState.loginStore.userLoginInfo
      let currentClientId = '' // 定义对应环境下的clientId
      if (!_userLoginInfo.clientId && !_userLoginInfo.testClientId) {
        // 两个环境都没有
        envType = envTypeData.noneEnv
        currentClientId = ''
        defaultEnvOfTwo = null
      } else {
        if (_userLoginInfo.clientId && _userLoginInfo.testClientId) {
          envType = envTypeData.allEnv
          // 两个环境都有，默认展示正式环境 --defaultEnvOfTwo表示两个环境下展示哪一个环境
          if (EnvOfTwo && EnvOfTwo === 'test') {
            defaultEnvOfTwo = EnvOfTwo
            currentClientId = _userLoginInfo.testClientId
          } else {
            defaultEnvOfTwo = 'master'
            currentClientId = _userLoginInfo.clientId
          }
        } else {
          if (_userLoginInfo.clientId) {
            envType = envTypeData.master
            currentClientId = _userLoginInfo.clientId
            defaultEnvOfTwo = null
          } else {
            envType = envTypeData.test
            currentClientId = _userLoginInfo.testClientId
            defaultEnvOfTwo = null
          }
        }
      }
      commit('CHANGE_SETTING', {
        key: 'envType',
        value: envType,
      })
      commit('CHANGE_SETTING', {
        key: 'currentClientId',
        value: currentClientId,
      })
      commit('CHANGE_SETTING', {
        key: 'defaultEnvOfTwo',
        value: defaultEnvOfTwo,
      })
    },
  },
}



// WEBPACK FOOTER //
// ./src/store/login_store.js